package com.yanqu.road.server.pb;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.entity.activity.*;
import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.silkroad.SilkRoadPlaceProgressInfo;
import com.yanqu.road.entity.activity.silkroad.UserSilkRoadData;
import com.yanqu.road.entity.activity.union.RechargeUserInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserCard;
import com.yanqu.road.entity.player.UserDailyData;
import com.yanqu.road.entity.servercenter.IntroductionShieldConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.activity.CardProto;
import com.yanqu.road.pb.activity.SilkRoadProto;
import com.yanqu.road.pb.activity.TurntableProto;
import com.yanqu.road.pb.activity.union.UnionRechargeUserProto;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityPb {

	public static ActivityProto.ActivityConditionTempMsg.Builder parseActivityConditionTempMsg(ActivityConditionInfo activityConditionInfo, String language){
		if(null == activityConditionInfo){
			return null;
		}
		ActivityProto.ActivityConditionTempMsg.Builder msg = ActivityProto.ActivityConditionTempMsg.newBuilder();
		msg.setActivityId(activityConditionInfo.getActivityId());
		msg.setConditionId(activityConditionInfo.getConditionId());
		msg.setType(activityConditionInfo.getType());
		msg.setReward(activityConditionInfo.getRewardStr());
		msg.setMasterReward(activityConditionInfo.getMasterRewardStr());
		msg.setDesc(ServerLanguageMgr.getContent(activityConditionInfo.getDesc(), language)); // 需要切语言
		msg.setTitle(ServerLanguageMgr.getContent(activityConditionInfo.getTitle(), language));// 需要切语言
		for(BigInteger param : activityConditionInfo.getParamList()){
			msg.addParamList(param.toString());
		}
		msg.setExtendParam(activityConditionInfo.getExtendParam());
		if (activityConditionInfo.getType() == eGamePlayerEventType.ExpressionActivityChoose.getValue()) {
			msg.setExtendParam(ServerLanguageMgr.getContent(activityConditionInfo.getExtendParam(), language));
		}
		msg.setDeputyMasterReward(activityConditionInfo.getDeputyMasterRewardStr());
		msg.setEliteReward(activityConditionInfo.getEliteRewardStr());
		msg.setSubText(ServerLanguageMgr.getContent(activityConditionInfo.getSubText(), language)); // 需要切语言
		msg.setIsOneBuy(ServerLanguageMgr.getContent(activityConditionInfo.getIsOneBuy(), language)); // 需要切语言
		return msg;
	}

	public static ActivityProto.ActivityConfigTempMsg.Builder parseActivityConfigTempMsg(ActivityConfig activityConfig, boolean needTranslate,String language) {
		if (null == activityConfig) {
			return null;
		}
		ActivityProto.ActivityConfigTempMsg.Builder msg = ActivityProto.ActivityConfigTempMsg.newBuilder();
		msg.setActivityId(activityConfig.getActivityId());
		msg.setId(activityConfig.getId());
		msg.setParam(activityConfig.getParam());
		msg.setValue(activityConfig.getValue());
		if (needTranslate){				//翻译一下
			msg.setValue(ServerLanguageMgr.getContent(activityConfig.getValue(),language));
		}
		return msg;
	}

	public static ActivityProto.ActivityTempMsg.Builder parseActivityTempMsg(ActivityInfo activityInfo, String language) {
		if(null == activityInfo){
			return null;
		}
		ActivityProto.ActivityTempMsg.Builder msg = ActivityProto.ActivityTempMsg.newBuilder();
		msg.setActivityId(activityInfo.getActivityId());
		msg.setResetType(Integer.parseInt(activityInfo.getParam2()));
		msg.setActivityType(activityInfo.getType());
		msg.setChildType(activityInfo.getChildType());
		msg.setBeginShowTime(activityInfo.getBeginShowTime());
		msg.setEndShowTime(activityInfo.getEndShowTime());
		msg.setBeginTime(activityInfo.getBeginTime());
		msg.setEndTime(activityInfo.getEndTime());
		msg.setActivityName(ServerLanguageMgr.getContent(activityInfo.getActivityName(), language)); //需要切语言
		msg.setActivityDesc(ServerLanguageMgr.getContent(activityInfo.getActivityDesc(), language)); //需要切语言
		msg.setConditionText(ServerLanguageMgr.getContent(activityInfo.getParam3(), language)); //需要切语言
		msg.setUnit(ServerLanguageMgr.getContent(activityInfo.getParam4(), language)); //需要切语言
		msg.setIcon(activityInfo.getIcon());
		msg.setPic(activityInfo.getParam1());
		msg.setGoToScene(ServerLanguageMgr.getContent(activityInfo.getParam5(), language));
		msg.setOrder(activityInfo.getOrder());
		msg.setExpandParam(ServerLanguageMgr.getContent(activityInfo.getParam6(), language));
		msg.addAllServerList(activityInfo.getServerIdList());
		msg.addAllServerList2(activityInfo.getServerIdList2());
		msg.setUnionGroup(activityInfo.getUnionGroup());
		return msg;
	}

	public static ActivityProto.ActivityConditionDataMsg.Builder parseActivityConditionDataMsg(UserActivityConditionData userActivityConditionData) {
		if (null == userActivityConditionData) {
			return null;
		}
		ActivityProto.ActivityConditionDataMsg.Builder msg = ActivityProto.ActivityConditionDataMsg.newBuilder();
		msg.setConditionId(userActivityConditionData.getConditionId());
		msg.setIsGetReward(userActivityConditionData.isGetReward());
		if (eGamePlayerEventType.LiveShowTime.getValue() == userActivityConditionData.getType()) {//直播任务客户端写错了，将错就错服务端这边下发转成分钟数
			BigInteger value = userActivityConditionData.getValue().divide(BigInteger.valueOf(DateHelper.MINUTE_MILLIONS));
			msg.setValue(value.toString());
		}else {
			msg.setValue(userActivityConditionData.getValue().toString());
		}
		msg.setModifyTime(userActivityConditionData.getModifyTime());
		msg.setParam(userActivityConditionData.getParam());
		return msg;
	}

	public static ActivityProto.ActivityDataMsg.Builder parseActivityDataMsg(BaseActivityData baseActivityData) {
		if(null == baseActivityData){
			return null;
		}
		ActivityProto.ActivityDataMsg.Builder msg = ActivityProto.ActivityDataMsg.newBuilder();
		msg.setActivityId(baseActivityData.getActivityId());
		for (UserActivityConditionData userActivityConditionData : baseActivityData.getUserActivityConditionDataList()) {
			ActivityProto.ActivityConditionDataMsg.Builder dataMsg = parseActivityConditionDataMsg(userActivityConditionData);
			if(null != dataMsg) {
				msg.addDataList(dataMsg);
			}
		}

		return msg;
	}

	public static ActivityProto.ActivityDataListMsg.Builder syncActivityConditionData(int activityId, List<UserActivityConditionData> userActivityConditionDataList) {
		ActivityProto.ActivityDataListMsg.Builder listMsg = ActivityProto.ActivityDataListMsg.newBuilder();
		if (userActivityConditionDataList.size() > 0) {
			ActivityProto.ActivityDataMsg.Builder dataMsg = ActivityProto.ActivityDataMsg.newBuilder();
			dataMsg.setActivityId(activityId);
			for (UserActivityConditionData userActivityConditionData : userActivityConditionDataList) {
				ActivityProto.ActivityConditionDataMsg.Builder data = parseActivityConditionDataMsg(userActivityConditionData);
				dataMsg.addDataList(data);
			}
			listMsg.addDataList(dataMsg);
		}
		return listMsg;
	}

	public static ActivityProto.ActivityDataListMsg.Builder parseActivityDataListMsg(List<BaseActivityData> baseActivityDataList,
																					 List<ActivityInfo> activityInfoList, Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap,
																					 Map<Integer, Map<Integer, MallInfo>> activityMallInfoMap, Map<Integer, Map<String, ActivityConfig>> allActivityConfigMap, String language) {
		ActivityProto.ActivityDataListMsg.Builder listMsg = ActivityProto.ActivityDataListMsg.newBuilder();
		for (BaseActivityData baseActivityData : baseActivityDataList) {
			if (baseActivityData.getUserActivityConditionDataList().size() > 0) {
				ActivityProto.ActivityDataMsg.Builder dataMsg = parseActivityDataMsg(baseActivityData);
				listMsg.addDataList(dataMsg);
			}
		}

		if (null != activityInfoList) {
			for (ActivityInfo activityInfo : activityInfoList) {
				ActivityProto.ActivityTempMsg.Builder activityMsg = parseActivityTempMsg(activityInfo, language);
				if(null != activityMsg) {
					listMsg.addActivityList(activityMsg);
				}

				if(null != activityConditionInfoMap){
					List<ActivityConditionInfo> tempConditionList = activityConditionInfoMap.get(activityInfo.getActivityId());
					if(null != tempConditionList) {
						for (ActivityConditionInfo activityConditionInfo : tempConditionList) {
							ActivityProto.ActivityConditionTempMsg.Builder conditionMsg = parseActivityConditionTempMsg(activityConditionInfo, language);
							if (null != conditionMsg) {
								listMsg.addConditionList(conditionMsg);
							}
						}
					}
				}

				if(null != activityMallInfoMap){
					Map<Integer, MallInfo> tempMallMap = activityMallInfoMap.get(activityInfo.getActivityId());
					if(null != tempMallMap){
						for(MallInfo mallInfo : tempMallMap.values()){
							CommonProto.MallTempMsg.Builder mallMsg = CommonPb.parseMallTempMsg(mallInfo, language);
							if (null != mallMsg) {
								listMsg.addActivityMallInfoList(mallMsg);
							}
						}
					}
				}

				if (null != allActivityConfigMap) {
					Map<String, ActivityConfig> tempActivityConfigMap = allActivityConfigMap.get(activityInfo.getActivityId());
					boolean needTranslate = false;
					if (activityInfo.getType() == eActivityType.RMBMallActivity.getValue() || activityInfo.getType() == eActivityType.SystemOpenRMBMallActivity.getValue()) {    //现金礼包加一下翻译
						needTranslate = true;
					}
					if (activityInfo.getType() == eActivityType.WitchDraw.getValue()){
						needTranslate = true;
					}
					if (null != tempActivityConfigMap) {
						for (ActivityConfig activityConfig : tempActivityConfigMap.values()) {
							if (activityInfo.getType() == eActivityType.QiXiActivity.getValue() && activityConfig.getParam().equals("QIXI_BROADCASTING_CONTENT")){
								needTranslate = true;
							}
							if (activityInfo.getType() == eActivityType.GuaGiftsActivity.getValue() && activityConfig.getParam().equals("GUAGIFTS_BROADCASTING_CONTENT")){
								needTranslate = true;
							}
							ActivityProto.ActivityConfigTempMsg.Builder builder = parseActivityConfigTempMsg(activityConfig,needTranslate,language);
							if (null != builder) {
								listMsg.addConfigList(builder);
							}
						}
					}
				}
			}
		}
		return listMsg;
	}

	public static CardProto.PrivilegeCardMsg.Builder parsePrivilegeCardMsg(UserCard userCard){
		if(null == userCard){
			return null;
		}
		CardProto.PrivilegeCardMsg.Builder msg = CardProto.PrivilegeCardMsg.newBuilder();
		msg.setMonthCardEndTime(userCard.getMonthCardEndTime());
		msg.setGetMonthCardRewardTime(userCard.getGetMonthCardRewardTime());
		msg.setYearCardEndTime(userCard.getYearCardEndTime());
		msg.setGetYearCardRewardTime(userCard.getGetYearCardRewardTime());
		msg.setWeekCardEndTime(userCard.getWeekCardEndTime());
		msg.setWeekCardExperiment(userCard.isWeekCardExperiment());
		return msg;
	}

	public static TurntableProto.TurntableLotteryRecordTempMsg.Builder parseTurntableLotteryRecordTempMsg(TurntableLotteryRecord turntableLotteryRecord){
		if(null == turntableLotteryRecord){
			return null;
		}
		TurntableProto.TurntableLotteryRecordTempMsg.Builder msg = TurntableProto.TurntableLotteryRecordTempMsg.newBuilder();
		msg.setRecordUid(turntableLotteryRecord.getRecordUid());
		msg.setUserId(turntableLotteryRecord.getUserId());
		msg.setNickName(turntableLotteryRecord.getNickName());
		msg.setGoodsId(turntableLotteryRecord.getGoodsId());
		msg.setCount(turntableLotteryRecord.getCount());
		msg.setRecordTime(turntableLotteryRecord.getRecordTime());
		return msg;
	}

	public static TurntableProto.TurntableLotteryRecordListMsg.Builder parseTurntableLotteryRecordListMsg(List<TurntableLotteryRecord> turntableLotteryRecordList){
		TurntableProto.TurntableLotteryRecordListMsg.Builder listMsg = TurntableProto.TurntableLotteryRecordListMsg.newBuilder();
		if(null != turntableLotteryRecordList){
			for(TurntableLotteryRecord turntableLotteryRecord : turntableLotteryRecordList){
				TurntableProto.TurntableLotteryRecordTempMsg.Builder msg = parseTurntableLotteryRecordTempMsg(turntableLotteryRecord);
				if(null != msg){
					listMsg.addDataList(msg);
				}
			}
		}
		return listMsg;
	}


	public static ActivityProto.ActivityConditionUnionMemberDataTempMsg.Builder parseActivityConditionUnionMemberDataTempMsg(UserActivityConditionData userActivityConditionData) {
		if(null == userActivityConditionData){
			return null;
		}
		ActivityProto.ActivityConditionUnionMemberDataTempMsg.Builder msg = ActivityProto.ActivityConditionUnionMemberDataTempMsg.newBuilder();
		msg.setUserId(userActivityConditionData.getUserId());
		msg.setNickName(UserMgr.getNickName(userActivityConditionData.getUserId()));
		msg.setValue(userActivityConditionData.getValue().toString());
		msg.setModifyTime(userActivityConditionData.getModifyTime());
		return msg;
	}

	public static ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder parseActivityConditionUnionMemberDataListMsg(List<UserActivityConditionData> userActivityConditionDataList) {
		ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder listMsg = ActivityProto.ActivityConditionUnionMemberDataListMsg.newBuilder();
		if(null != userActivityConditionDataList){
			for(UserActivityConditionData userActivityConditionData : userActivityConditionDataList) {
				ActivityProto.ActivityConditionUnionMemberDataTempMsg.Builder msg = parseActivityConditionUnionMemberDataTempMsg(userActivityConditionData);
				if(null != msg){
					listMsg.addDataList(msg);
				}
			}
		}
		return listMsg;
	}

	public static ActivityProto.UnionRankMemberConditionDataTempMsg.Builder parseUnionRankMemberConditionDataTempMsg(UserActivityConditionData userActivityConditionData) {
		if(null == userActivityConditionData){
			return null;
		}
		ActivityProto.UnionRankMemberConditionDataTempMsg.Builder msg = ActivityProto.UnionRankMemberConditionDataTempMsg.newBuilder();
		msg.setUserId(userActivityConditionData.getUserId());
		msg.setValue(userActivityConditionData.getValue().toString());
		msg.setModifyTime(userActivityConditionData.getModifyTime());
		UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(userActivityConditionData.getUserId(), GameServer.getInstance().getServerId());
		msg.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
		return msg;
	}

	public static ActivityProto.UnionRankMemberConditionDataListMsg.Builder parseUnionRankMemberConditionDataListMsg(List<UserActivityConditionData> userActivityConditionDataList) {
		ActivityProto.UnionRankMemberConditionDataListMsg.Builder listMsg = ActivityProto.UnionRankMemberConditionDataListMsg.newBuilder();
		if(null != userActivityConditionDataList){
			for(UserActivityConditionData userActivityConditionData : userActivityConditionDataList) {
				ActivityProto.UnionRankMemberConditionDataTempMsg.Builder msg = parseUnionRankMemberConditionDataTempMsg(userActivityConditionData);
				if(null != msg){
					listMsg.addDataList(msg);
				}
			}
		}
		return listMsg;
	}

	public static UnionRechargeUserProto.UnionRechargeUserRespMsg.Builder parseUnionRechargeUserMsg(int activityId,UnionActivityConditionData data){
		UnionRechargeUserProto.UnionRechargeUserRespMsg.Builder respMsg = UnionRechargeUserProto.UnionRechargeUserRespMsg.newBuilder();
		Map<Integer,List<RechargeUserInfo>> infoMap = JSON.parseObject(data.getInfo(), new TypeReference<Map<Integer, List<RechargeUserInfo>>>(){});
		if(infoMap == null)infoMap = new ConcurrentHashMap<>();
		respMsg.setActivityId(activityId);
		for(Integer conditionId : infoMap.keySet()){
			UnionRechargeUserProto.UnionRechargeUserConditionTempMsg.Builder conditionMsg = UnionRechargeUserProto.UnionRechargeUserConditionTempMsg.newBuilder();
			conditionMsg.setConditionId(conditionId);
			for(RechargeUserInfo info : infoMap.get(conditionId)){
				UnionRechargeUserProto.UnionRechargeUserTempMsg.Builder userMsg = UnionRechargeUserProto.UnionRechargeUserTempMsg.newBuilder();
				userMsg.setUserId(info.getUserId());
				userMsg.setNickName(UserMgr.getNickName(info.getUserId()));
				userMsg.setTotalRecharge(info.getTotalRecharge());
				userMsg.setIsGetReward(info.isGetReward());
				conditionMsg.addUserList(userMsg);
			}
			respMsg.addConditionList(conditionMsg);
		}
		return respMsg;
	}

	public static UnionRechargeUserProto.UnionRechargeUserSyncMsg.Builder parseUnionRechargeUserSyncMsg(int activityId,UnionActivityConditionData data){
		UnionRechargeUserProto.UnionRechargeUserSyncMsg.Builder respMsg = UnionRechargeUserProto.UnionRechargeUserSyncMsg.newBuilder();
		Map<Integer,List<RechargeUserInfo>> infoMap = JSON.parseObject(data.getInfo(), new TypeReference<Map<Integer, List<RechargeUserInfo>>>(){});
		if(infoMap == null)infoMap = new ConcurrentHashMap<>();
		respMsg.setActivityId(activityId);
		for(Integer conditionId : infoMap.keySet()){
			UnionRechargeUserProto.UnionRechargeUserConditionTempMsg.Builder conditionMsg = UnionRechargeUserProto.UnionRechargeUserConditionTempMsg.newBuilder();
			conditionMsg.setConditionId(conditionId);
			for(RechargeUserInfo info : infoMap.get(conditionId)){
				UnionRechargeUserProto.UnionRechargeUserTempMsg.Builder userMsg = UnionRechargeUserProto.UnionRechargeUserTempMsg.newBuilder();
				userMsg.setUserId(info.getUserId());
				userMsg.setNickName(UserMgr.getNickName(info.getUserId()));
				userMsg.setTotalRecharge(info.getTotalRecharge());
				userMsg.setIsGetReward(info.isGetReward());
				conditionMsg.addUserList(userMsg);
			}
			respMsg.addConditionList(conditionMsg);
		}
		return respMsg;
	}

	public static ActivityProto.ActivityGetRewardRespMsg.Builder parseActivityGetRewardRespMsg(ActivityRewardResult rewardResult){
		if(null == rewardResult){
			return null;
		}
		ActivityProto.ActivityGetRewardRespMsg.Builder msg = ActivityProto.ActivityGetRewardRespMsg.newBuilder();
		msg.setRet(rewardResult.getRet());
		msg.setActivityId(rewardResult.getActivityId());
		msg.setConditionId(rewardResult.getConditionId());
		if (0 == rewardResult.getRet()) {
			msg.setReward(rewardResult.getReward());
		}
		return msg;
	}

	public static CardProto.WeekCardDailyDataSyncMsg.Builder parseWeekCardDailyDataSyncMsg(UserDailyData userDailyData){
		CardProto.WeekCardDailyDataSyncMsg.Builder msg = CardProto.WeekCardDailyDataSyncMsg.newBuilder();
		if(null == userDailyData){
			msg.setTrystTimes(0);
			msg.setTravelTimes(0);
		}else {
			msg.setTrystTimes(userDailyData.getWeekCardTrystTimes());
			msg.setTravelTimes(userDailyData.getWeekCardTravelTimes());
		}
		return msg;
	}

	public static ActivityProto.IntroductionShieldTempMsg.Builder parseIntroductionShieldTempMsg(IntroductionShieldConfig data){
		if(null == data){
			return null;
		}
		ActivityProto.IntroductionShieldTempMsg.Builder msg = ActivityProto.IntroductionShieldTempMsg.newBuilder();
		msg.setActivityType(data.getActivityType());
		msg.setChildType(data.getChildType());
		return msg;
	}

	public static ActivityProto.IntroductionShieldListMsg.Builder parseIntroductionShieldListMsg(List<IntroductionShieldConfig> dataList){
		ActivityProto.IntroductionShieldListMsg.Builder listMsg = ActivityProto.IntroductionShieldListMsg.newBuilder();
		if(null != dataList){
			for(IntroductionShieldConfig data : dataList){
				ActivityProto.IntroductionShieldTempMsg.Builder msg = parseIntroductionShieldTempMsg(data);
				if(null != data){
					listMsg.addDataList(msg);
				}
			}
		}
		return listMsg;
	}

	public static SilkRoadProto.SilkRoadUserDataSyncMsg.Builder parseSilkRoadUserBuilder(UserSilkRoadData userActivityData, SilkRoadPlaceProgressInfo placeProgressInfo) {
		SilkRoadProto.SilkRoadUserDataSyncMsg.Builder builder = SilkRoadProto.SilkRoadUserDataSyncMsg.newBuilder();
		builder.setActivityId(userActivityData.getActivityId());
		builder.setScore(userActivityData.getScore());
		builder.setRelicAchieve(userActivityData.getAchievement());

		builder.setPlaceId(placeProgressInfo.getPlaceId());
		builder.setRouteProgress(placeProgressInfo.getRoutProgress());
		builder.setCurrentRound(placeProgressInfo.getRound());

		builder.addAllEventIds(userActivityData.getEventIds());
		builder.setLastSea(userActivityData.getNowSea());
		for (Map.Entry<Integer, Integer> entry : userActivityData.getRefloatItemMap().entrySet()) {
			SilkRoadProto.SilkRoadRelicTemp.Builder relicsBuilder = SilkRoadProto.SilkRoadRelicTemp.newBuilder();
			relicsBuilder.setRelicId(entry.getKey());
			relicsBuilder.setNum(entry.getValue());

			builder.addRelics(relicsBuilder);
		}
		return builder;
	}
}
